package main

import (
	"flag"
	"gitee.com/yysf_xin/dplog/pkg/agg"
	"gitee.com/yysf_xin/dplog/pkg/config"
	"gitee.com/yysf_xin/dplog/pkg/extracter/fex"
	"gitee.com/yysf_xin/dplog/pkg/extracter/java"
	"gitee.com/yysf_xin/dplog/pkg/push"
	"gitee.com/yysf_xin/dplog/pkg/slog"
	log2 "log"
	"time"
)

type Option struct {
	File   string
	Config string
	Java   java.Opt
	Option slog.Option
}

var (
	Opt = Option{}
)

func main() {

	flag.StringVar(&Opt.File, "f", "", "use slog file")
	flag.StringVar(&Opt.Config, "c", "", "use config file")
	java.InitFlag(&Opt.Java)
	slog.InitFlag(&Opt.Option)

	flag.Parse()

	var aggConfig config.Agg
	aggConfig.Level = 2
	aggConfig.Size = 20
	aggConfig.Threshold = 0.6
	aggConfig.Expire = config.Duration(5 * time.Minute)
	aggConfig.Span = config.Duration(5 * time.Minute)

	em1 := agg.New(&aggConfig, &push.Server{})

	em := java.NewMybatis(em1, []string{"org.apache.ibatis.logging.jdbc.BaseJdbcLogger"})

	c := slog.NewCollect(&Opt.Option, fex.NewMakeLog(&Opt.Java, ""), em)

	if Opt.File != "" {
		r, err := slog.NewFileReader(Opt.File, c)

		if err != nil {
			panic(err)
		}

		defer r.Close()

		now := time.Now()
		r.Run()

		log2.Println("read file take", time.Now().Sub(now).String())
		//log2.Println("total msg", em1.count, "gc len", gc.Len(false))
		//log2.Println("stat hit", gc.HitCount(), "rate", gc.HitRate(), "miss", gc.MissCount(), "lookup", gc.LookupCount())

		//for _, v := range gc.GetALL(false) {
		//	counter := v.(*[]*Counter)
		//	log2.Println(len(*counter), "==============================group")
		//	for _, m := range *counter {
		//		msg := m.Msg.GetRef()
		//		log2.Println(m.Count, msg.GetLevel(), msg.GetLogger(), util.BytesToString(msg.GetMessage()))
		//	}
		//}
	}
}
